﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;

namespace YArchitech.MEP.Electric.CableLay
{
	internal class AutomaticLayingCommon
	{
		public static List<Element> GetEquipmentByConnectors(List<Connector> connectorList)
		{
			List<Element> list = new List<Element>();
			foreach (Connector connector in connectorList)
			{
				ConnectorSet allRefs = connector.AllRefs;
				if (allRefs.Size > 0)
				{
					foreach (object obj in allRefs)
					{
						Element owner = ((Connector)obj).Owner;
						if (!list.Contains(owner, AutomaticLayingCommon.equipmentComparer))
						{
							list.Add(owner);
						}
					}
				}
			}
			return list;
		}

		public static Dictionary<Connector, MEPCurve> GetMEPCurveByConnectors(List<Connector> connectorList)
		{
			Dictionary<Connector, MEPCurve> dictionary = new Dictionary<Connector, MEPCurve>();
			foreach (Connector connector in connectorList)
			{
				ConnectorSet allRefs = connector.AllRefs;
				if (allRefs.Size > 0)
				{
					foreach (object obj in allRefs)
					{
						Connector connector2 = (Connector)obj;
						Element owner = connector2.Owner;
						if (owner is MEPCurve && !dictionary.Values.Contains(owner, AutomaticLayingCommon.equipmentComparer))
						{
							dictionary.Add(connector2, owner as MEPCurve);
						}
					}
				}
			}
			return dictionary;
		}

		public static FamilyInstance GetEquipmentByMEPCurveConnector(Connector connector)
		{
			FamilyInstance result = null;
			ConnectorSet allRefs = connector.AllRefs;
			if (allRefs.Size <= 0)
			{
				return result;
			}
			foreach (object obj in allRefs)
			{
				Element owner = ((Connector)obj).Owner;
				if (owner is FamilyInstance)
				{
					result = (owner as FamilyInstance);
					break;
				}
			}
			return result;
		}

		private static EquipmentComparer equipmentComparer = new EquipmentComparer();
	}
}
